﻿// CMakeProjectOpenssh.cpp: 定义应用程序的入口点。
//

#include "CMakeProjectOpenssh.h"

//using namespace std;
//#include "./3rd/openssl/include/openssl/hmac.h"

#include <string>
#include <vector>
#include <memory>
#include <algorithm>


#include <openssl/ssl.h>
#include <openssl/evp.h>
#include <openssl/err.h>
#include <openssl/bn.h>
#include <openssl/md5.h>
#include <openssl/sha.h>
#include <openssl/hmac.h>
#include <openssl/comp.h>
#include <openssl/rsa.h>
#include <openssl/dsa.h>
#include <openssl/des.h>
#include <openssl/aes.h>
#include <openssl/hmac.h>
#include <string.h>

#include <openssl/dsa.h>
#include <openssl/des.h>
#include <openssl/dh.h>
#include <openssl/ec.h>
#include <openssl/ecdsa.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/bio.h>
#include <openssl/ossl_typ.h>
//#pragma comment(lib,"libssl-1_1-x64.lib")
//#pragma comment(lib,"libcrypto-1_1-x64.lib")
//https://cpp.hotexamples.com/zh/site/file?hash=0xa4911a69fd499cdb13a191695ad55f4011a50a54bf083c047f253b30f6c4b7cd&fullName=source-master/openssl_base.c&project=beike2020/source
#define  COMM_LEN	128
#define  SHA1_LEN	160
#define  LINE_LEN 	512
#define  MAX1_LEN 	1024
#define  MAX2_LEN 	2048
#define  MAX3_LEN 	3072
#define  MAX4_LEN 	4096
#define  MAX5_LEN	5120
//#define OPENSSL_NO_DH

BIO* bio_err;
void openssl_dh_crypt()
{
	//BIO* b;
	//DH* d1, * d2;
	//int i, len1, len2;
	//unsigned char skey1[COMM_LEN], skey2[COMM_LEN];

	//d1 = DH_new();
	//d2 = DH_new();
	//DH_generate_parameters_ex(d1, 64, DH_GENERATOR_2, NULL);
	//DH_check(d1, &i);
	//printf("\nDH key size: %d\n", DH_size(d1));
	//DH_generate_key(d1);

	//d2->p = BN_dup(d1->p);
	//d2->g = BN_dup(d1->g);
	//DH_generate_key(d2);
	//DH_check_pub_key(d1, d1->pub_key, &i);
	//len1 = DH_compute_key(skey1, d2->pub_key, d1);
	//len2 = DH_compute_key(skey2, d1->pub_key, d2);
	//if ((len1 != len2) || (memcmp(skey1, skey2, len1) != 0)) {
	//	printf("DH_compute_key err!\n");
	//	DH_free(d1);
	//	DH_free(d2);
	//	return;
	//}

	//b = BIO_new(BIO_s_file());
	//BIO_set_fp(b, stdout, BIO_NOCLOSE);
	//DHparams_print(b, d1);

	//BIO_free(b);
	//DH_free(d1);
	//DH_free(d2);

}
#include "tests/TestEcdh.h"
#include "tests/TestComp.h"
#include "tests/TestRsa.h"
#include "tests/TestDH.h"
//#include "tests/TestClientSSL.h"
int main()
{
	std::string sss = "2222222";

	std::cout << "Hello CMake." << std::endl;

	//TestClientSSL::testSSLClient();

	/*
	Openssl EVP(high-level cryptographic functions[1])提供了丰富的密码学中的各种函数。
	Openssl 中实现了各种对称算法、摘要算法以及签名/验签算法。EVP 函数将这些具体的算
	法进行了封装。
	EVP 主要封装了如下功能函数：
	1）实现了base64 编解码BIO；
	2）实现了加解密BIO；
	3）实现了摘要BIO；
	4）实现了reliable BIO；
	5）封装了摘要算法；
	6）封装了对称加解密算法；
	7）封装了非对称密钥的加密(公钥)、解密(私钥)、签名与验证以及辅助函数；
	7）基于口令的加密(PBE)；
	8）对称密钥处理；
	9）数字信封：数字信封用对方的公钥加密对称密钥，数据则用此对称密钥加密。
	发送给对方时，同时发送对称密钥密文和数据密文。接收方首先用自己的私钥解密密钥
	密文，得到对称密钥，然后用它解密数据。
	10）其他辅助函数。
	
	*/

	TestEcdh::testRand();
	TestEcdh::testRand();

	//DH see 
	TestDH::testDH();


	//DSA todo

	//RSA
	TestRsa::testRSASignAndCheck();

	TestRsa::testRSAEncryptAndDecrypt();

	TestRsa::generatorKey();

	//Comp
	TestComp::testComp();

	//Ecdh
	TestEcdh::testDigest();

	TestEcdh::testBase64EncodingDecode();
	TestEcdh::testBigNum();

	TestEcdh::testTXTDB();

	TestEcdh::testRand();

	TestEcdh::testBIOCrypt();
	TestEcdh::testECCECDH();

	/*
	HMAC 用于保护消息的完整性，它采用摘要算法对消息、填充以及秘密密钥进行混合
运算。在消息传输时，用户不仅传送消息本身，还传送HMAC 值。接收方接收数据后也进
行HMAC 运算，再比对MAC 值是否一致。由于秘密密钥只有发送方和接收方才有，其他
人不可能伪造假的HMAC 值，从而能够知道消息是否被篡改。
ssl 协议中用HMAC 来保护发送消息，并且ssl 客户端和服务端的HMAC 密钥是不同的，
即对于双方都有一个读MAC 保护密钥和写MAC 保护密钥
	
	*/
#ifdef HMAC_TEST

	//hmac(sha256)
	HMAC_CTX *ctx= HMAC_CTX_new();
	
	const EVP_MD* engine = EVP_sha256(); // it can also be: EVP_md5(), EVP_sha1, etc
	const char* key = "https://github.com/fengbingchun";
	const char* data = "fc8627ba57542d514b443332332d35304d2d522d3139303530313032000000000000000057542d514b443332332d35304d2d542d31393035303130320000000000000000026168620000100029916c7ac803e187cd7c1f4134517407b24ff90c8534e360a3f8d1d5c2cb0565b787aa71ca6a131a88261182a6b9bbb1c4749bfe7af469b8fa79cb3f9a339d3805e1a3dc07b43a76eb83cca20233ffdae6cb84615ac6b437a1d1a66ada6e322560f951c6c8567ead2db0230fca0963ca26f492c7a93fb98b0bf77dad908c3abd1cc0b277d35f7affcb02973556fbf99513159ea4b0e2d34912dd9c8ca334e1943002715b5dffae0b6e4f23e607bfa635198c598c2ff046234949f142fc8b9b8223e7ce1b90eff61142a004c5cf959d0fb23ede764bf15e10cc34c44800376329bd83ca4d4cb0b55fb254444b571cd9b4262a0209e84a50734aec02bb636179b398de61700ef90e09cac783334d44b9aa5dedadb102be13ed0e7d27deee15265eb92527c5320fb0b11388a2f759d0dc82a324e248217750dcacb9b86a11c10258f31c61201363b36ca332f640d271157cbfc4dc8bf02b0fca123d67b917128873582751acb5ebef69fb36adc23a179faa9d3b134a43db8d332f38b05152c357aadc80075f1e36669f307770edc4d9fd661a360baae75600840a0e738ddcaefba121ca839d922316d1c834fe3cf88a985da14e7a158f03a64e8291dba1ba8e43b954d3e3dfee92ff454bed8808f2f95b6befda6e3ee74757764da09001ce33dcba0da20ae319f0a886582730c842992ddc09accf97043a49edec39829d2ce7a307ce9aed5f79f19d0d11af0fda6a2a62a393679659aca78e2347bd407957d7a386e4cd3cb7bdca3b89cd4ce3daeea154a4420b71fa5586de2238c3d1d16cd5be74be29a9f0b6bb66d9ecfa6d8498efbcb44706c3b1518837d86c6eb438ac3d6d9afece0d86465614f051dbed34830c4a021c89a3ece486f8b8fc47d79824e738ec35a632123b471d976af7825aab3adb8f158c83bebf4cd5934c7be85a5b989006413616e1676638eeb2277a8c2b47b6a0257ae8bc971e565637086e3a23871f8a0ff31747ce7cd8b8b56cb382d29cc3465e3463ad0a740871e6e25d982fb92d346eff5a212debe3e758180e755264c22d4ba2805980f4a004963faa75791d5a5b554768cf7e93abd9daf8a6b92f5d07bd9dfc0cb2d1845011d647323c6dae8ca7c0080d8a6369f01d1b3c24a9420c413db0526afe5dc312c2acc68c9949f916530682e08a903061219f09ede792120424aa19543c2dc80db83d70c89a80cac71446d22335404ac0a82014d0e56b2e9433c9dab1ee6da85fff12bdc55a8da9134824db6e575777672a66c36e92f5af9008391a0f07efee2fbf2374e8786259eb36073d8cad040255c0547744498616454f347357d52d43fde75c3f7e0720c22a68c97e4a62bc0dcb7ecf441625c173108e061cc507c0b16c633653239d9fa259e5b1fb5f58ce3d61e2f1f733e20fb98133db14a0dea78d54bbbbbabed7c0b47fb68213f029e7f4863108bb910c971bd79ee90955dce8bfd093a09bfe0d0c31b954aea90c40521a4fbf71f99cc620d085b3c5f2b9b4579896a9374e566da892dac38aa57eaae9d66fb1e33dbd27b2975eb0a7a67bf3980937e2678757f8ea11de43494d7d36de13fe11c15eb9dad394437ad8323b33c3b081d1c5bcaed4a301cd70035c35eb9a77c1b14a797cc7e24f9ebbca146bf242de7a68baf42c6a32111e9c4375f3841bca94526ae68de27323556bf28a03d1435b2cb64e633cf2cd4b5755759ea79712bfc12194f47320978e3e0cd502854808e08410fa45162e64d2b8af2eb0b52d433905562f4b63eb8db5fae96101f75e95f2f8e1f018658f3c5d16435c2f0cd47ea9eedea89ca8886d84689851574f38265f530e8b772cbbc8215cfe8428ff2ae5c3bc370f8b39d26329b86b1a4a4a652b17d3d888b33c90eb7c382f2252ebba8bdf574ee1109ef0affa1b4481398b2743a82b16f0f99400ce20a20704171149c62cdf1d23be3e3b46c56042d2f0022f7a975378a9be5dd32a723b1000b72191ccca6d8af09420eeb94e584b3b33b1d48a66cd581fae4ae0451d4163e4eae31ed38aeb723a27f061d636657992d73fb8132e8f65980928bb3093bc5374779861ae0fec585aca4b18624aa0fc2bd34996ff976ad5aab31af94a2094526d2daa73446924cf6e1de00b507dbd7d5e0a3c7902838e52190bf085b537810053578c6fb69e9b51aac73aee5f0906247504bb476847cc845fa88e56577e8d83d526cb891c1f181dedb886f900eab15baf55a170bb118a56d90a6be66832d9b490b1dc4e76dbb59c7c8e3d9755aa2f9c80455a83790b77984857393d940fb05c1e9f9cd6e5926d577ea27e3dee35fc0682727979e99789638ac9b05667ae185a590a39d30d5f19d408d1b3fa8688d81b177c1e91ce989b0fac6a7ede0eb4bba17af4c950ad9f4bce052103f103474c7e43cc476e6d7fba74020d78da420ffbee9c5b38030b7c7a193c905d6c38d520396fcfa5d98f5e10c8e6047b9f4b12fd3f8f5c33b53951eff59e27d65834f9f30e788c620976a81488926d66c7628676e9c5904fb8ed5198d189c61fca383067b9ba883c8df3cb3f54cd4b6cb3da39623d3151d76070b886a76772f8795d1a04fe96475c92db6869b902c1f21dda88f0c4beec381c1e16b19eef59f870cfc350f695a5621598358a9b7af25c7272a8bcc59d74d02a20423188032a06887366326119d2fd4b2e67550f0dc49a1ac56e88cf76e1be1c92c97fd8a45624af75bdd709552324f73ddda7c6eb0dfda91a650da004fe98d7a6ef6aa7843976fcbc3c5fc040fa195c2801bd953743f55b3c7bc5f7d684f2dafb081c26fcc022de652ae115b9d53fa017543abe27dd4921d7fe980fbb9c65f57f082de51200d2bcdfa4d27252a952accd6aaa32a6c0d6f283a4cb16454ec2632f8cd7884502906ce2457e424696f2eef8fa57a84e4ab3f0d8ad0788751014d9c3b0aa097647ba5f018dec58cb40f07c183d8d461a8b542690504471a64c23ffa670362d67b6223bce4665fd971542c0d6a6810a27aea976a2d5d0997e6d900e296a5b8a987e4c105622e37d9f5d1fbb03d1e9388a125e47270ff8b94c1d96211201eb6349c1140b8ab18c912dec530a7452d793d95666fdd47e09ec2c3c7e9060a9d06666ad14a1ec99905093a45972d13f0a0a38a47618cb624deac7a546f4af928d858bfabb1f1d0fb2b8c9dc13264d50b20d6f3da37a5269c3c883d13bef7e8809ec0a8ba3002ed58b2943351c3164791547684866b1dc89ac619dd3e5173ec7d731cacd2fce54ed9d658644d8a5a6108a18aa45cc63c219b859d80a8ac39cb3cffe35f84136e94cf45cf94db6e289c39048a872d6c84a6758190dab2a845a1c24de26bb3741e24662381945efa6565f00a35c872c37c0cf4ce2360c0f333eb01e3869ce4906417050d6a3c2164f012fda0d2e77fcac0631b62e374481d19ee8afbcce032a486c2b4faf851da81f5f666c8fa10c3927811eca8ce28d47698b7d2e50078aff141bfa41c0ed2d2cd02104bbf401b112d9adf64cce15299da3c5a92f7ffdc690a7f44b9b031875353240b05ccf4894dbb54b166d43ef0d632d3ac28f789d0f6fc22f0d0418b396920bd8a4e4050eade572655d715b70c77aebe9177f555bee12be182b759f2fb5765881b32ff4eed108ccc20358df2a4dd81e9be270b9e4d2c2f38945e583fcc05d4a647710a926ad23957c836fc8085e35390ae045c11411c01d948cc545bfe4ba77c39b201e35873e7b5dadecb16175bdf00d99341d0f734c5decc9da826c6213ec7a6dc90e21aeb51e359f5b478c4645987e5cb25fa8c3d0a8ded551a9d592e52a473cf2b5bee97da3bd455ebdd3b899b3c45bad165e0ee99b3263366d9fab910e24e434090cf718c554bfdb2772ac2f1c0ecf905147c26e0140a0dab3e074b0dbf93e319d6784fa945b259622b7a040fcc7da25b1a42b90e0566430a1ebd29271bdb55c64b6cdf36baec0010608e0abc58c007fa8854d1691b68b3b465732ba91956275e972e0e132687de6d9c7b99aad6433df9b9e043238498d8e04a714e7d972e3c777fc448d955fc8be4c8ede7c2f37952c330576e034abd04c513254c4f55625d432cbfe307d5b97ac2c243b804978204dc8ba59c99df9ebe20cf43e8c126549a727e38cd980b1764709e05aa81e1e37864c99a9776e4dd9a3a3549100557a5bf1ac3ff5055306629c305461052dbc9421de8699c13bd598c4e8a7cd8e018078879d4fad2349a246ff922638967266f2b72e37d324c25bf87d4d86a723d68113206ae25b9d84cd8d3f1de15f4f81f860ca87713e4a2453e51c58a2ef2d0b3f4decfbfe1a0dc44fe4fa9aae4d1fb4c8c00b9884b9358592a3400d55be13acf484404b78b2aa6aa8db02c17d1b5585955328633ed2162a99bd7d7ca2b1382cbf07a0a46e13bf6bc1902ca5c80d581dcd62a2df5c42c167b3425a44c23b295b7dfe804a6d91ed09a880c3100a589387fad4bf656b23d347d5c58e7bae9533ab2a58a6df8ffd6447c97361d1f4d29e7dcb59145860f5b76f4e20bb87d6150974627d6272e0ba05d09013e1322c911c1cfdbaa0892d8b410d5f8d44bdf71afc74897134a78f3fcae7739ae166dee3ee734d9fc9bb8911561dfd25f4097f8ac2bd7d577d6ce84f9247b028063d57bd2952ec62654c898123703b02ecc6ebe47eb1300f33596a1888462a1facc2b2a0637a75b1d4c3bb9413de1acccf8809b9c778aed73e300e9951a4a0d3f9681a06c589ea10bcd70a5aa307f61177d0fcdde66548f3aa208d3f57e6010c7d24216dd26ff2788ffc0f877814a5a92f645302619d063c5505b24b8b266367fcbb4457ba497f401887eee7156fd6fbbf9cf81c954b4631177c693a185748515713bf085d0fb09a9ec5d87952180096f22d5b11059ad42d8f21ec7b5ef66b107580400e64c015487abff3d35c7770125fe7105d873d84b38cccdc9a66969aab138ce73d6beaed5fbfa7a5935425967ffdd97650eef01311b94a58e7bad92c9c38a3fff5ae72e204669cbcce194da89d7461ed5c743d784283a4aa29df02f743005fa439f139cd8c477dcbe668e2be133228c5cfa553201b00c86a85582f113d39acdc499f1fe94e5799373624a984a23648ac92426100c4c6a684e388c9a12aaf487b373bf939ad59aef19bcc798db9da8c7a476e2ef5770ef966f118df687e1fd85e186a7e025f5c133632027c43505ecbe8212181fc1883e4ab71848d0256b160bc96e11db9e00db3dd8e73a495c75f7ebe8c1f58acc73e1a60a984805d8695702d061f32d0d88e2aca67be2a35c9d6b9a051bff1dc4d55f425a5da2c923992a90b36659f30185c22cf48ede4f04d71be69ecfcd5751871f5ec4fe7f60a2842fda05231eae56708340d1a618b61c3ed331913b4a16c28f07b568b65eb2fc0989f11d9fc6f122334f21d9cfa16795e270f2e8e5f5e9f9b8eade5ff7f54df2c03b3053dca47d3d2f612ae08be6e6ab217d2245bc0f869ab2bd2dd2a8d30403d817f37aa3ddfde70561ecb7f453c459b3a4f7bf64eb0347ceebd6178fa60b5ec15749b629690baafd880844b8ad05375add48a5cef03479a129e73a42fd0899019eaa64d9474e83d1c2e32484057862af636e8ec60e091b59cc9ac6e94c38cc6ffb719d503d8a1191587e8a6d24741444a893c0fb20038fc0345ec991b43b6eebec3c4059a68e796d833722d91bd1d656997b58001b644d6e9f899b503a0f1fd35633299bd316f3588b1921d4937c89521d33b2251bf2f5f17dd3acab31ece2226a1";//"https://blog.csdn.net/fengbingchun";
	std::unique_ptr<unsigned char[]> output(new unsigned char[EVP_MAX_MD_SIZE]);
	unsigned int output_length;

	HMAC_Init_ex(ctx, key, strlen(key), engine, nullptr);
	HMAC_Update(ctx, reinterpret_cast<const unsigned char*>(data), strlen(data));

	HMAC_Final(ctx, output.get(), &output_length);

	HMAC_CTX_free(ctx);

	fprintf(stdout, "output length: %d\noutput result:", output_length);

	std::for_each(output.get(), output.get() + output_length, [](unsigned char v) { fprintf(stdout, "%02X", v); });
	fprintf(stdout, "\n");

#endif // HMAC_TEST
	//DH
	//https://stackoverflow.com/questions/71551116/openssl-3-diffie-hellman-key-exchange-c
	
	//DH* dh = DH_new();

	///* Parameters */
	//const unsigned char bin_p[16] = { 0 };
	//const int bin_p_size = 16;

	//const unsigned char bin_g[16] = { 0 };
	//const int bin_g_size = 16;

	//dh->p = BN_bin2bn(bin_p, bin_p_size, NULL);
	//dh->g = BN_bin2bn(bin_g, bin_g_size, NULL);

	///* Private key generation */
	//const char hex_priv_key[16] = { 0 };

	//BN_hex2bn(&dh->priv_key, hex_priv_key);

	///* Public key generation */
	//DH_generate_key(dh);

	///* Derive */
	//unsigned  char shared_key[16] = { 0 };
	//const BIGNUM* peer_pub_key;
	//int shared_key_size = DH_compute_key(shared_key, peer_pub_key, dh);

	//【c++ primer】死循环
	/*for (unsigned int index = 10; index >= 0;index--) {
		std::cout << index << std::endl;
	}*/

	/*unsigned int index = 11;
	while (index > 0) {
		--index;
		std::cout << index << std::endl;
	}

	std::cout << "over" << std::endl;*/

	while (1);
	return 0;
}

